package com.xxx.client;

import com.xxx.client.handler.RemoteCallClient;
import com.xxx.client.loadbalance.LastResponseTimeLoadbalancer;
import com.xxx.client.loadbalance.Loadbalancer;
import com.xxx.client.loadbalance.RoundRobinLoadbalancer;
import com.xxx.client.registry.ChannelInfo;
import com.xxx.client.registry.ServerNodeChangeListener;
import com.xxx.registry.NodeChangeListenerUtils;
import com.xxx.service.DemoService;

public class ClientBootStrap {


    public static void main(String[] args) throws InterruptedException {


        RemoteCallClient rpcClient = new RemoteCallClient();
        rpcClient.createProxies(DemoService.class, "com.xxx.service.DemoService.demo");

        if (RemoteCallClient.getInstance().getAvailableServers().isEmpty()) {
            System.out.println("=============[Init]No any servers to connecting retry after 5 seconds============");

            Thread.sleep(5000);
            main(args);
            return;
        }

        NodeChangeListenerUtils.registerPathChildrenCacheListener("/", new ServerNodeChangeListener());

        while (true) {

            Loadbalancer loadbalancer = new LastResponseTimeLoadbalancer();

            ChannelInfo channelInfo = loadbalancer.getClient();
            if (channelInfo == null) {
                System.out.println("=============[Reconnect]No any servers to connecting retry after 5 seconds============");
                Thread.sleep(5000);
                continue;
            }

            DemoService demoServiceProxy = (DemoService) channelInfo.getClientProxy();
            Thread.sleep(5000);

            try {
                System.out.println(demoServiceProxy.demo("i am client"));
            } catch (Exception e) {

            }


        }


    }


}
